package com.ciccwm.auth.util;

import java.util.Collection;

/**
 * 安全上下文持有者，用于存储当前用户信息.
 */
public final class SecurityContextHolder {
    private static final ThreadLocal<Long> USER_ID_HOLDER = new ThreadLocal<>();
    private static final ThreadLocal<String> USERNAME_HOLDER = new ThreadLocal<>();
    private static final ThreadLocal<Collection<String>> AUTHORITIES_HOLDER = new ThreadLocal<>();

    private SecurityContextHolder() {
        throw new UnsupportedOperationException("This is a utility class and cannot be instantiated");
    }

    /**
     * 获取当前用户ID.
     */
    public static Long getUserId() {
        return USER_ID_HOLDER.get();
    }

    /**
     * 设置当前用户ID.
     */
    public static void setUserId(Long userId) {
        USER_ID_HOLDER.set(userId);
    }

    /**
     * 获取当前用户名.
     */
    public static String getUsername() {
        return USERNAME_HOLDER.get();
    }

    /**
     * 设置当前用户名.
     */
    public static void setUsername(String username) {
        USERNAME_HOLDER.set(username);
    }

    /**
     * 获取当前用户权限.
     */
    public static Collection<String> getAuthorities() {
        return AUTHORITIES_HOLDER.get();
    }

    /**
     * 设置当前用户权限.
     */
    public static void setAuthorities(Collection<String> authorities) {
        AUTHORITIES_HOLDER.set(authorities);
    }

    /**
     * 清除当前用户信息.
     */
    public static void clear() {
        USER_ID_HOLDER.remove();
        USERNAME_HOLDER.remove();
        AUTHORITIES_HOLDER.remove();
    }
} 
